$(function () {
    var nameLike
    var categoryId
    var uid = getCookie('uid')
    var token = getCookie('token')
    // 数据加载到页面
    function show_good(res) {
        let str = ''
        let {
            code,
            data
        } = res
        let {
            result
        } = data
        console.log(result)
        result.forEach(item => {
            str += `
            <div class="col-sm-4 col-md-4 col-lg-3 goods">
                <div class="thumbnail">
                <img src="${item.pic}" alt="你太美" class='wid_50'>
                <div class="caption">
                    <h3 class="over_dian">${item.name}</h3>
                    <p>原价${item.originalPrice}&nbsp 现价${item.minPrice}</p>
                    <p>
                        销量${item.numberSells} &nbsp 库存<i class='store'>${item.stores}</i>
                    </p>
                    <p>
                        <a href="javascript:;" class="btn btn-primary add-car" role="button" good-id='${item.id}'>购物车</a>
                        <a href="./detail.html?id=${item.id}" class="btn btn-default" role="button">查看详情</a>
                    </p>
                </div>
                </div>
            </div>
            `
        });
        $('.cotent').html(str)

    }
    // 获取商品请求
    function getgoodlist(obj) {
        $.ajax({
            type: 'post',
            url: 'https://api.it120.cc/conner/shop/goods/list/v2',
            data: obj

        }).then(res => {
            if (res.code != 700) {
                show_good(res)
            } else {
                alert('暂无数据')
            }
        })
    }



    // 获取分类请求
    function get_feilie(obj) {
        $.ajax({
            type: 'get',
            url: 'https://api.it120.cc/conner/shop/goods/category/all',
            data: obj
        }).then(res => {
            show_good(res)
        })
    }
    // 首次加载页面
    getgoodlist({})

    // 模糊查询
    $('.serach').click(function () {
        nameLike = $('.namelike').val()
        getgoodlist({
            nameLike
        })
    })


    // 获取分类列表
    $.ajax({
        type: 'get',
        url: 'https://api.it120.cc/conner/shop/goods/category/all',

    }).then(res => {
        let str = `
                <div>
                    <span fl_id=''>
                        <img src="" alt="">
                    
                    </span>
                    <span>全部商品</span>
                </div>
                `
        const {
            data
        } = res
        console.log(res)
        data.forEach(item => {
            str += `
                <div fl_id='${item.id}'>
                    <span fl_id=''>
                        <img src="${item.icon}" alt="">
                    
                    </span>
                    <span>${item.name}</span>
                </div>
                `

        })
        $('.fenlei_list').html(str)
    })





    $('.fenlei_list').delegate('div', 'click', function () {
        let fl_id = $(this).attr('fl_id')
        categoryId = fl_id
        console.log(fl_id)
        getgoodlist({
            categoryId
        })

    })

    // 排序事件
    $('.paixu>div').delegate('span', 'click', function () {
        let data_sort = $(this).attr('data-sort')
        getgoodlist({
            orderBy: data_sort,
            nameLike,
            categoryId
        })
    })



    //购物车事件
    $('.cotent').delegate('.add-car', 'click', function () {

        if (!uid) {
            location.href = './login.html'
        } else {
            let id = $(this).attr('good-id')
            let store = $(this).parent().prev().find('.store').text()
            store=Number(store)
            if (store) {
                token = getCookie('token')
                $.ajax({
                    type: 'post',
                    url: 'https://api.it120.cc/conner/shopping-cart/add',
                    data: {
                        goodsId: id,
                        number: 1,
                        token
                    }
                }).then(res => {
                    if (res.code == 2000) {
                        let resoult = confirm('当前用户登陆信息失效，是否去登陆')
                        if (resoult) {
                            location.href = './login.html'
                        }
                    }
                    if (res.code == 0) {
                        let resoult = confirm('加入成功，是否查看购物车')
                        if (resoult) {
                            location.href = './car.html'
                        }
                    }

                })
            }else{
                alert('当前商品没有库存，无法加入库存，亲！！可以收藏偶')
            }

        }
    })

})